package org.loofer.ext.component.logger;

import android.content.Context;
import android.os.Environment;
import org.loofer.ext.utils.FileUtils;
import java.io.File;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class LoggerFile {
    public static boolean hasConfigured = false;

    public static abstract class Log4jWrapper {
        public abstract void debug(Object obj);

        public abstract void debug(Object obj, Throwable th);

        public abstract void error(Object obj);

        public abstract void error(Object obj, Throwable th);

        public abstract void error(Throwable th);

        public abstract void fatal(Object obj);

        public abstract void fatal(Object obj, Throwable th);

        public abstract void fatal(Throwable th);

        public abstract void info(Object obj);

        public abstract void info(Object obj, Throwable th);

        public abstract void trace(Object obj);

        public abstract void trace(Object obj, Throwable th);

        public abstract void warn(Object obj);

        public abstract void warn(Object obj, Throwable th);

        public abstract void warn(Throwable th);
    }

    public static boolean configure() {
        if (!Environment.getExternalStorageState().equals("mounted")) {
            return false;
        }
        try {
            LogConfigurator logConfigurator = new LogConfigurator();
            logConfigurator.setFileName(LogConstants.PATH + LogConstants.FILE_NAME);
            logConfigurator.setRootLevel(Level.ALL);
            logConfigurator.setFilePattern("%d - [%p::%c] - %m%n");
            int isDebugMode = 0;
            if (isDebugMode != 0) {
                logConfigurator.setMaxFileSize(5242880);
            } else {
                logConfigurator.setMaxFileSize(524288);
            }
            Log.i("isDebugMode", "configure() flags===>>>>" + 0 + ",ApplicationInfo.FLAG_DEBUGGABLE===>>>" + 2 + ",isDebugMode==>>" + isDebugMode);
            logConfigurator.configure();
            hasConfigured = true;
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static boolean configure(Context context) {
        if (!Environment.getExternalStorageState().equals("mounted")) {
            return false;
        }
        try {
            boolean isDebugMode;
            LogConfigurator logConfigurator = new LogConfigurator();
            FileUtils.mkdirs(new File(LogConstants.PATH));
            logConfigurator.setImmediateFlush(true);
            logConfigurator.setFileName(LogConstants.PATH + LogConstants.FILE_NAME);
            logConfigurator.setRootLevel(Level.ALL);
            logConfigurator.setFilePattern("%d - [%p::%c] - %m%n");
            int flags = 0;
            try {
                flags = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).applicationInfo.flags;
            } catch (Exception e) {
                e.printStackTrace();
            }
            if ((flags & 2) != 0) {
                isDebugMode = true;
            } else {
                isDebugMode = false;
            }
            if (isDebugMode) {
                logConfigurator.setMaxFileSize(5242880);
            } else {
                logConfigurator.setMaxFileSize(524288);
            }
            Log.i("isDebugMode", "configure(Context context) flags===>>>>" + flags + ",ApplicationInfo.FLAG_DEBUGGABLE===>>>" + 2 + ",isDebugMode==>>" + isDebugMode);
            logConfigurator.configure();
            hasConfigured = true;
            return true;
        } catch (Exception e2) {
            return false;
        }
    }

    public static Log4jWrapper getLog4j(String str) {
        if (!Environment.getExternalStorageState().equals("mounted")) {
            return null;
        }
        if (hasConfigured || !configure()) {
            return new LogToFile(Logger.getLogger(str));
        }
        return new LogToFile(Logger.getLogger(str));
    }
}
